using System;
using System.Drawing;
using System.Windows.Forms;
using WinformTemplete.Modules.About;
using WinformTemplete.Modules.Blog;
using WinformTemplete.Modules.Blog.Services;
using WinformTemplete.Modules.Settings;

namespace WinformTemplete
{
    public partial class MainForm : Form
    {
        private readonly IBlogService _blogService;
        private Form _currentModuleForm;
        private Button _currentSelectedButton;
        private bool _isSidebarCollapsed = false;
        private const int _sidebarExpandedWidth = 175;
        private const int _sidebarCollapsedWidth = 50;
        private const int _animationSteps = 10;
        private const int _animationInterval = 10;

        public MainForm(IBlogService blogService)
        {
            _blogService = blogService;
            InitializeComponent();
            InitializeNavigation();
        }

        private void InitializeNavigation()
        {
            // 添加按钮点击事件
            btnBlog.Click += (s, e) => LoadModule(new BlogForm(_blogService), btnBlog);
            btnSettings.Click += (s, e) => LoadModule(new SettingsForm(), btnSettings);
            btnAbout.Click += (s, e) => LoadModule(new AboutForm(), btnAbout);


            // 默认加载博客模块
            LoadModule(new BlogForm(_blogService), btnBlog);
        }

        private void btnCollapse_Click(object sender, EventArgs e)
        {
            _isSidebarCollapsed = !_isSidebarCollapsed;
            AnimateSidebar();
        }

        private void AnimateSidebar()
        {
            int startWidth = asidePanel.Width;
            int endWidth = _isSidebarCollapsed ? _sidebarCollapsedWidth : _sidebarExpandedWidth;
            int step = (endWidth - startWidth) / _animationSteps;

            var timer = new System.Windows.Forms.Timer { Interval = _animationInterval };
            timer.Tick += (s, e) =>
            {
                int newWidth = asidePanel.Width + step;
                if ((step > 0 && newWidth >= endWidth) || (step < 0 && newWidth <= endWidth))
                {
                    newWidth = endWidth;
                    timer.Stop();
                    timer.Dispose();
                    UpdateButtonVisibility();
                }
                asidePanel.Width = newWidth;
            };
            timer.Start();
        }

        private void UpdateButtonVisibility()
        {
            if (_isSidebarCollapsed)
            {
                btnBlog.Text = "📝";
                btnSettings.Text = "⚙️";
                btnAbout.Text = "ℹ️";
                btnCollapse.Text = "▶";
            }
            else
            {
                btnBlog.Text = "📝 博客管理";
                btnSettings.Text = "⚙️ 系统设置";
                btnAbout.Text = "ℹ️ 关于";
                btnCollapse.Text = "◀";
            }
        }

        private void LoadModule(Form moduleForm, Button selectedButton)
        {
            // 移除当前模块
            if (_currentModuleForm != null)
            {
                _currentModuleForm.Close();
                _currentModuleForm.Dispose();
            }

            // 更新按钮选中状态
            if (_currentSelectedButton != null)
            {
                _currentSelectedButton.BackColor = Color.FromArgb(45, 45, 48);
            }
            selectedButton.BackColor = Color.FromArgb(0, 120, 215);
            _currentSelectedButton = selectedButton;

            // 加载新模块
            moduleForm.TopLevel = false;
            moduleForm.FormBorderStyle = FormBorderStyle.None;
            moduleForm.Dock = DockStyle.Fill;
            contentPanel.Controls.Add(moduleForm);
            moduleForm.Show();
            _currentModuleForm = moduleForm;
        }

       
    }
}
